﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using WanMeiCommon;
using WanMeiModel;

namespace WanMeiService
{
    /// <summary>
    /// 通信基类。所有通信类，只负责接收用户请求和返回请求，不参与任何数据库，redis业务操作。
    /// 此类也封装了一些用户网关的基础通知方法,整合各个HUB的通知方法，方便客户端查找
    /// </summary>
    public abstract class BaseHub : Hub
    {
        /// <summary>
        /// 公共的通知指定客户端信息
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="connectionId"></param>
        protected virtual void NoticeMsg(string obj,string connectionId)
        {
            Clients.Client(connectionId).NoticeMsg(obj);
        }
        protected virtual void CheckConnect(bool result)
        {
            Clients.Client(Context.ConnectionId).CheckConnect(result);
        }

        #region UserHub
        protected virtual void ReceiveRegisterAndLogin(bool isSuccess, string msg)
        {
            Clients.Client(Context.ConnectionId).ReceiveRegisterAndLogin(ResultManager.CreateReuslt(isSuccess,msg));
        }
        protected virtual void ReceiveJumpConnect(bool isSuccess, string msg)
        {
            Clients.Client(Context.ConnectionId).ReceiveJumpConnect(ResultManager.CreateReuslt(isSuccess, msg));
        }

        #endregion

        #region MainHub
        protected virtual void ConnectedReceive<T>(bool isSuccess, T model)
        {
            Clients.Client(Context.ConnectionId).ConnectedReceive(ResultManager.CreateResult<T>(isSuccess,model));
        }
        #endregion
        #region ChatHub
        /// <summary>
        /// 消息广播到所有人方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isSuccess"></param>
        /// <param name="model"></param>
        protected virtual void SendMsgReceiveForAll<T>(bool isSuccess, T model)
        {
            Clients.All.SendMsgReceiveForAll(ResultManager.CreateResult<T>(isSuccess, model));
        }
        /// <summary>
        /// 私聊消息通知方法(发送者和发送人都通知)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isSuccess"></param>
        /// <param name="model"></param>
        /// <param name="connectionIds"></param>
        protected virtual void SnedMsgReceiveForPrivate<T>(bool isSuccess, T model, List<string> connectionIds)
        {
            Clients.Clients(connectionIds).SnedMsgReceiveForPrivate(ResultManager.CreateResult<T>(isSuccess, model));
        }
        #endregion


        /// <summary>
        /// 检测连接
        /// </summary>
        /// <param name="username"></param>
        public void CheckConnect(string username)
        {
            var result = PublicDataInfomation.Instance.GetUserByUserId(username) == null ? false : true;
            CheckConnect(result);
        }
        /// <summary>
        /// 用户链接进来的事件
        /// </summary>
        /// <returns></returns>
        public override Task OnConnected()
        {
            Console.WriteLine(string.Format("{0}连接到服务器!", Context.ConnectionId));
            //通知当前链接的用户连接成功
            //Clients.Client(Context.ConnectionId).Receive("链接服务器成功");
            return base.OnConnected();
        }

        /// <summary>
        /// 用户断开连接的事件
        /// </summary>
        /// <param name="stopCalled"></param>
        /// <returns></returns>
        public override Task OnDisconnected(bool stopCalled)
        {
            PublicDataInfomation.Instance.RemoveUser(Context.ConnectionId);
            return base.OnDisconnected(stopCalled);
        }
    }
}
